package com.duowei.diandan.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.duowei.diandan.R;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.databinding.InputPlateNoFragmentBinding;
import com.duowei.diandan.impl.OnButtonGroupClickListener;

public class InputPlateNoFragment extends Fragment implements View.OnClickListener {
    public static final String TAG = "InputPlateNoFragment";

    private InputPlateNoFragmentBinding mBinding;

    private OnButtonGroupClickListener mOnButtonGroupClickListener;

    public void setOnButtonGroupClickListener(OnButtonGroupClickListener onButtonGroupClickListener) {
        mOnButtonGroupClickListener = onButtonGroupClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_input_plate_no, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.num0.setOnClickListener(this);
        mBinding.num1.setOnClickListener(this);
        mBinding.num2.setOnClickListener(this);
        mBinding.num3.setOnClickListener(this);
        mBinding.num4.setOnClickListener(this);
        mBinding.num5.setOnClickListener(this);
        mBinding.num6.setOnClickListener(this);
        mBinding.num7.setOnClickListener(this);
        mBinding.num8.setOnClickListener(this);
        mBinding.num9.setOnClickListener(this);
        mBinding.numDelete.setOnClickListener(this);

        mBinding.backButton.setOnClickListener(v -> {
            if (mOnButtonGroupClickListener != null) {
                mOnButtonGroupClickListener.onLeftButtonClick();
            }
        });

        mBinding.confirmButton.setOnClickListener(v -> {
            Settings.PLATE_NO = mBinding.plateNoView.getText().toString();

            if (mOnButtonGroupClickListener != null) {
                mOnButtonGroupClickListener.onRightButtonClick();
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(mBinding.plateNoView.getText().toString())) {
            hideConfirmButton();
        } else {
            showConfirmButton();
        }
    }

    @Override
    public void onClick(View v) {
        if (v instanceof TextView) {
            TextView tv = (TextView) v;
            input(tv.getText().toString());
        } else {
            input(null);
        }

        int length = mBinding.plateNoView.getText().toString().length();
        if (length > 0) {
            if (!mBinding.confirmButton.isEnabled()) {
                showConfirmButton();
            }
        } else {
            if (mBinding.confirmButton.isEnabled()) {
                hideConfirmButton();
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private void input(String num) {
        String curVal = mBinding.plateNoView.getText().toString();
        if (!TextUtils.isEmpty(curVal)) {
            if (num != null) {
                mBinding.plateNoView.setText(curVal + num);
            } else {
                mBinding.plateNoView.setText(curVal.substring(0, curVal.length() - 1));
            }
        } else {
            mBinding.plateNoView.setText(num);
        }
    }

    private void showConfirmButton() {
        mBinding.confirmButton.animate()
                .alpha(1)
                .setDuration(300)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mBinding.confirmButton.setEnabled(true);
                    }
                });
    }

    private void hideConfirmButton() {
        mBinding.confirmButton.animate()
                .alpha(0)
                .setDuration(300)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mBinding.confirmButton.setEnabled(false);
                    }
                });
    }
}
